package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ThemeContent;
import cn.wolfcode.trip.base.domain.ThemeDetail;
import cn.wolfcode.trip.base.mapper.ThemeContentMapper;
import cn.wolfcode.trip.base.mapper.ThemeDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IThemeDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ThemeDetailServiceImpl implements IThemeDetailService {

    @Autowired
    private ThemeDetailMapper themeDetailMapper;

    @Autowired
    private ThemeContentMapper contentMapper;

    /**
     * 新增和编辑
     * @param
     */
    public void saveOrUpdate(ThemeDetail themeDetail) {
        if(themeDetail.getId()!=null){
            //更新明细
            themeDetailMapper.updateByPrimaryKey(themeDetail);
            //获得明细内容
            ThemeContent themeContent = themeDetail.getThemeContent();
            //主键关联
            themeContent.setId(themeDetail.getId());
            //更新明细内容
            contentMapper.updateByPrimaryKey(themeContent);

        }else{
            //保存明细
            themeDetailMapper.insert(themeDetail);
            //获得明细内容
            ThemeContent themeContent = themeDetail.getThemeContent();
            //主键关联
            themeContent.setId(themeDetail.getId());
            //保存明细内容
            contentMapper.insert(themeContent);
        }
    }

    /**
     * 根据明细id查询明细内容
     * @param id
     * @return
     */
    public ThemeContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页
     * @param qo
     * @return
     */
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<ThemeDetail> list = themeDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }
}
